package com.example.waterfall;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.waterfall.adapter.WaterfallAdapter;
import com.example.waterfall.model.BitmapInfo;
import com.example.waterfall.util.ContentTask;
import com.example.waterfall.util.ImageFetcher;
import com.example.waterfall.util.NetworkUtil;
import com.example.waterfall.util.ParseService;

import com.example.waterfall.util.ContentTask.TaskListener;
import com.example.waterfall.view.XListView;
import com.example.waterfall.view.XListView.IXListViewListener;
import com.huewu.pla.lib.internal.PLA_AdapterView;
import com.huewu.pla.lib.internal.PLA_AdapterView.OnItemClickListener;

/**
 * 瀑布流主界面
 * 
 * @author huangrongcai
 * 
 */
public class MainActivity extends FragmentActivity implements
		IXListViewListener, OnItemClickListener {

	// ///////////////////////////
	// handler 接受非主线程的消息，并处理之
	// ///////////////////////
	private Handler handler = new Handler() {
		//
	};

	private static final String TAG = "MainActivity";
	static final String PAR_KEY = "Bitmap_info";
	private WaterfallAdapter adapter;
	private ImageFetcher mImageFetcher;
	private XListView waterfallView;// 可以把它当成一个listView

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_waterfall);

		waterfallView = (XListView) findViewById(R.id.list);
		waterfallView.setPullRefreshEnable(false);
		waterfallView.setPullLoadEnable(true);
		mImageFetcher = new ImageFetcher(this, handler);
		adapter = new WaterfallAdapter(this, waterfallView, handler,mImageFetcher);
		waterfallView.setXListViewListener(this);
		waterfallView.setOnItemClickListener(this);

		AddItemToContainer(null);//首次加载
		waterfallView.setAdapter(adapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mImageFetcher.pauseTasks(false);
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onStop() {
		super.onStop();
		//停止下载线程
		mImageFetcher.pauseTasks(true);
	}

	@Override
	public void onRefresh() {
		// TODO 下拉刷新，未实现
	}

	@Override
	public void onLoadMore() {
		Log.i(TAG, "加载更多");
		// TODO 此处，传递参数，而数，每页的条数，获取一新加载数据集
		AddItemToContainer(null);

	}

	/**
	 * 添加内容
	 * 
	 * @param data
	 *            服务器请求接口(http:www.//page++/)
	 */
	private void AddItemToContainer(Object data) {
		
		String url = "http://192.168.1.131:8893/WaterfallWebService.asmx/";
		url = url +"GetWorksByCondtion";
		ContentTask task = new ContentTask(this, listener);
		task.execute(url);
	}

	TaskListener listener = new TaskListener() {
		@Override
		public void onPreExecute() {
			// TODO 执行前s
			//检查网络
			if(!NetworkUtil.isAvailable(getApplicationContext())){
				Toast.makeText(MainActivity.this, "网络，服务器忙", 0).show();
			}
			
		}
		
		@Override
		public List<BitmapInfo> doInBackground(String... params) {
			if(NetworkUtil.isAvailable(getApplicationContext())){
				//获取数据，请求地址params[0]
				try {
					String str ="请求参数";
					String xml = NetworkUtil.getStringFromUrl(params[0],str,null);
					Log.d(TAG, xml);
					parseXml(xml);
//					parseNewsJSON(json);
					
				} catch (ClientProtocolException e) {
					
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return null;
		}
		
		/**
		 * 解析xml数据
		 * @param xml 被解析xml
		 */
		private void parseXml(String xml) {
			BitmapInfo info  = new BitmapInfo();
			List<BitmapInfo> list = new ArrayList<BitmapInfo>();
			list = ParseService.parseXml(xml);
			
		}

		@Override
		public void onPostExecute(List<BitmapInfo> result) {
			waterfallView.stopLoadMore();
			adapter.addItemLast(result);
			adapter.notifyDataSetChanged();

		}
		

		
	};


	@Override
	public void onItemClick(PLA_AdapterView<?> parent, View view, int position,
			long id) {
		Toast.makeText(this, "点击" + position, 0).show();
		//传弟对象到另一个界面
		Intent intent = new Intent();
		
		BitmapInfo info = (BitmapInfo) adapter.getItem(position);
		Bundle b = new Bundle();
		b.putParcelable(PAR_KEY, info);
		intent.putExtras(b);
		
		intent.setClass(this, DetialActivity.class);
		startActivity(intent);

	}

}
